package com.frejoys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frejoys.common.dto.admin.ActivityAddDto;
import com.frejoys.common.dto.admin.ActivityUpDto;
import com.frejoys.common.dto.admin.DelIdDto;
import com.frejoys.common.dto.common.PageDto;
import com.frejoys.common.enums.EShowStatus;
import com.frejoys.common.enums.EStatus;
import com.frejoys.common.util.TimeUtil;
import com.frejoys.common.vo.admin.ActivityPageVo;
import com.frejoys.common.vo.common.SelectVo;
import com.frejoys.dao.entity.Activity;
import com.frejoys.dao.entity.Ad;
import com.frejoys.dao.mapper.ActivityMapper;
import com.frejoys.service.ActivityService;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ActivityServiceImpl  extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    @Override
    public List<SelectVo> getSelectList() {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Activity::getStatus, EStatus.on.getValue());
        List<Activity> activities = baseMapper.selectList(queryWrapper);
        List<SelectVo> collect = activities.stream().map(a -> new SelectVo(a.getType(), a.getName())).toList();
        return collect;
    }

    @Override
    public Map<Integer, String> getTypeNameMap() {
        Map<Integer, String> storeIdNameMap = new HashMap<>();
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Activity::getType, Activity::getName);
        queryWrapper.eq(Activity::getStatus, EStatus.on.getValue());
        List<Activity> activityList = baseMapper.selectList(queryWrapper);
        if (!activityList.isEmpty()) {
            storeIdNameMap = activityList.stream().collect(Collectors.toMap(Activity::getType, Activity::getName));
        }
        return storeIdNameMap;
    }


    @Override
    public IPage<ActivityPageVo> pageList(PageDto dto) {
        Page<Activity> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        IPage<Activity> activityIPage = baseMapper.selectPage(page, wrapper);
        IPage<ActivityPageVo> voIPage = activityIPage.convert(activity -> BeanUtil.copyProperties(activity, ActivityPageVo.class));
        return voIPage;
    }


    @Override
    public boolean addInfo(ActivityAddDto dto) {
        return save(new Activity() {{
            setName(dto.getName());
            setType(dto.getType());
            setStatus(EShowStatus.hide.getValue());
        }});
    }


    @Override
    public boolean upInfo(ActivityUpDto dto) {
        LambdaUpdateWrapper<Activity> wrapper = new UpdateWrapper().lambda();
        wrapper.eq(Activity::getId, dto.getId());

        if (StrUtil.isNotEmpty(dto.getName())) {
            wrapper.set(Activity::getName, dto.getName());
        }
        if (dto.getType() != null) {
            wrapper.set(Activity::getType, dto.getType());
        }

        return update(wrapper);
    }


    @Override
    public boolean delInfo(DelIdDto dto) {
        LambdaUpdateWrapper<Activity> adUpdateWrapper = new LambdaUpdateWrapper<>();
        adUpdateWrapper.set(Activity::getStatus, dto.getStatus());
        adUpdateWrapper.eq(Activity::getId, dto.getId());
        return update(adUpdateWrapper);
    }
}
